package com.mazaiting.manager.controller;

import com.mazaiting.common.core.domain.result.Option;
import com.mazaiting.common.core.ex.exceptions.BaseException;
import com.mazaiting.common.core.ex.exceptions.BusinessException;
import com.mazaiting.common.core.ex.exceptions.EmptyObjectException;
import com.mazaiting.manager.domain.form.DictItemForm;
import com.mazaiting.manager.domain.query.DictItemPageQuery;
import com.mazaiting.manager.domain.vo.dict.DictItemPageVO;
import com.mazaiting.manager.service.ISysDictItemService;
import com.mazaiting.mybatisplus.domain.vo.BasePageVO;
import com.mazaiting.web.auth.security.annotation.PrePermission;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 字典数据表前端控制器
 *
 * @author mazaiting
 * @since 2022-10-16
 */
@RestController
@RequestMapping("/manager/v1/dictItem")
@RequiredArgsConstructor
public class SysDictItemController {

    private final ISysDictItemService sysDictItemService;

    /**
     * 字典数据项分页列表
     * @param queryParams 查询参数
     * @return 分页数据
     */
    @PrePermission("sys:dict:item:query")
    @GetMapping
    public BasePageVO<DictItemPageVO> listDictItemPages(DictItemPageQuery queryParams) {
        return sysDictItemService.listDictItemPages(queryParams);
    }

    /**
     * 字典数据项表单
     * @param id 字典ID
     * @return 表单结果
     */
    @PrePermission("sys:dict:item:query")
    @GetMapping("/{id}/formData")
    public DictItemForm getDictItemFormData(@PathVariable Long id) throws EmptyObjectException {
        return sysDictItemService.getDictItemFormData(id);
    }

    /**
     * 新增字典数据项
     * @param dictItemForm 字典表单
     * @return true: 新增成功; false: 新增失败
     */
    @PrePermission("sys:dict:item:add")
    @PostMapping
    public boolean save(DictItemForm dictItemForm) throws BusinessException {
        return sysDictItemService.save(dictItemForm);
    }

    /**
     * 修改字典数据项
     * @param dictItemForm 字典表单
     * @return true: 更新成功; false: 更新失败
     */
    @PrePermission("sys:dict:item:update")
    @PutMapping
    public boolean update(DictItemForm dictItemForm) throws BaseException {
        return sysDictItemService.update(dictItemForm);
    }

    /**
     * 删除字典
     * @param ids 字典ID，多个以英文逗号(,)分割
     * @return true: 删除成功; false: 删除失败
     */
    @PrePermission("sys:dict:item:delete")
    @DeleteMapping("/{ids}")
    public boolean deleteByIds(@NotEmpty(message = "删除数据为空") @PathVariable String ids) {
        return sysDictItemService.deleteByIds(ids);
    }

    /**
     * 根据字典类型编码获取字典数据项
     * @param typeCode 字典类型编码
     * @return 选项
     */
    @GetMapping("/selectList")
    public List<Option<String>> getDictItemsByTypeCode(String typeCode) {
        return sysDictItemService.listDictItemsByTypeCode(typeCode);
    }


}
